package com.yimixian.app.address;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.yimixian.app.EventBusDomain.ShowAdSelectChange;
import com.yimixian.app.R;
import com.yimixian.app.adapter.PoiListViewAdapter;
import com.yimixian.app.adapter.PoiListViewAdapter2;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class SwitchSearchActivity extends Activity {

    private ListView lv_main;
    private ImageView iv_back;
    private View ll_right, loading_view;
    private PoiListViewAdapter poiAdapter;
    private ArrayList<PoiInfo> mList = new ArrayList<>();
    private PoiSearch mPoiSearch;
    private String city;
    private EditText et_search;
    private int pageNum = 0;
    private int pageSize = 20;
    private TextView footerView;
    private float dentity;
    private boolean isNeedPageLoad = false;
    private boolean isEndPage = false;
    private String keyWords = "写字楼";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_search);

        lv_main = (ListView) findViewById(R.id.lv_main);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        ll_right = findViewById(R.id.ll_right);
        loading_view = findViewById(R.id.loading_view);
        et_search = (EditText) findViewById(R.id.et_search);

        city = getIntent().getStringExtra("city");
        mPoiSearch = PoiSearch.newInstance();
//        searchMore();
        mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                loading_view.setVisibility(View.GONE);
                if(poiResult == null || poiResult.getAllPoi() == null){
                    lv_main.setVisibility(View.GONE);
                    UiUtils.showToast("暂无相关数据");
                    return;
                }
                if (poiResult.getAllPoi().size() < pageSize) {
//                    lv_main.removeFooterView(footerView);
                    isEndPage = true;
                    footerView.setText("没有更多了");
                } else {
                    footerView.setText("上滑加载更多");
                }
                if(pageNum == 0)
                    mList.clear();
                mList.addAll(poiResult.getAllPoi());
                if (mList.size() > 0) {
                    poiAdapter.notifyDataSetChanged();
                    lv_main.setVisibility(View.VISIBLE);
                    if(pageNum == 0)
                        lv_main.setSelection(0);
                } else {
                    lv_main.setVisibility(View.GONE);
                }
                pageNum++;
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }
        });

        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                keyWords = s.toString();
                if (keyWords.length() > 0) {
                    pageNum = 0;
                    isEndPage = false;
                    searchMore();
                } else {
                    lv_main.setVisibility(View.GONE);
                }
            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UiUtils.closeSoftKeyBoard(SwitchSearchActivity.this);
//                setResult();
                finish();
                overridePendingTransition(R.anim.activity_anim_stay, R.anim.slide_out_right);
            }
        });

        poiAdapter = new PoiListViewAdapter(this);
        poiAdapter.setList(mList);
        lv_main.setAdapter(poiAdapter);

        dentity = getResources().getDisplayMetrics().density;
        footerView = new TextView(this);
        footerView.setText("上滑加载更多...");
        footerView.setPadding(0, (int) (15 * dentity), 0, (int) (15 * dentity));
        footerView.setGravity(Gravity.CENTER);
        lv_main.addFooterView(footerView);

        lv_main.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == SCROLL_STATE_IDLE && isNeedPageLoad && !isEndPage) {
                    footerView.setText("加载中...");
                    searchMore();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                isNeedPageLoad = false;
                if (firstVisibleItem + visibleItemCount >= totalItemCount && !isEndPage) {
                    isNeedPageLoad = true;
                    footerView.setText("松开加载");
                }
            }
        });

        lv_main.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PoiInfo poiInfo = mList.get(position);
                if (poiInfo != null) {
                    ShortAddress shortAddress = new ShortAddress(poiInfo, poiInfo.location);
                    SharedPreferencesHelper.saveAddress(shortAddress, SharePreferenceKeys.CURRENT_ADDRESS);
                    ShortAddress shortNoLoginAddress = new ShortAddress(poiInfo, poiInfo.location);
                    SharedPreferencesHelper.saveAddress(shortNoLoginAddress, SharePreferenceKeys.CURRENT_NO_LOGIN_ADDRESS);
                    EventBus.getDefault().postSticky(new ShowAdSelectChange());
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });

    }

    private void searchMore(){
        mPoiSearch.searchInCity(new PoiCitySearchOption().city(city).keyword(keyWords).pageNum(pageNum).pageCapacity(pageSize));
//        if(pageNum == 0)
//            loading_view.setVisibility(View.VISIBLE);
    }
}
